package com.auto.tv.serial.comn.utils;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class CustomByteUtil {

    /**
     * 16进制的字符串表示转成字节数组
     *
     * @param hexString 16进制格式的字符串
     * @return 转换后的字节数组
     **/
    public static byte[] toByteArray(String hexString) {
        if (hexString == null || hexString.length() <= 0)
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static String floatToHexString(float num, int digit) {
        int num_int = BigDecimal.valueOf(num* digit)
                .setScale(0, BigDecimal.ROUND_HALF_DOWN)
                .intValue();
        return String.format("%04x", num_int); //2个字节
    }

    public static String hexTimeString() {
        long time = System.currentTimeMillis();
        String timestr = String.valueOf(time / 1000);
        int timeint = Integer.parseInt(timestr);
        return String.format("%08x", timeint); //4个字节
    }

    public static float hexByteTofloat(byte hex1, byte hex2, int digit) {
        byte[] num_byte = new byte[] { hex1, hex2 };
        String num_str = toHexString(num_byte, "");
        int num_int = Integer.parseInt(num_str.replaceAll("^0[x|X]", ""), 16);
        return (float) num_int / (float) digit;
    }

    public static int isOdd(int num) {
        return num & 1;
    }

    public static byte hexToByteArr(String inHex) {
        byte[] result;
        int hexlen = inHex.length();
        if (isOdd(hexlen) == 1) {
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result[j];
    }

    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    public static String byteToHex(int n) {
        String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
        if (n < 0) {
            n = n + 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexArray[d1] + hexArray[d2];
    }

    /**
     * 校验码
     *
     * @param input 需要转换的字节数组
     */
    public static int checkDigit(byte[] input) {
        if (input == null) return 0;
        int sum = 0;
        for (int i = 0; i < input.length; i++) {
            sum += input[i];
        }
        return sum & 0xff;
    }


    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param input     需要转换的字节数组
     * @param separator 分隔符
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] input, String separator) {
        if (input == null) return null;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length; i++) {
            if (separator != null && sb.length() > 0) {
                sb.append(separator);
            }
            String str = Integer.toHexString(input[i] & 0xff);
            if (str.length() == 1) str = "0" + str;
            sb.append(str);
        }
        return sb.toString();
    }

    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param input 需要转换的字节数组
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] input) {
        return toHexString(input, " ");
    }

    public static byte[] fromInt32(int input) {
        byte[] result = new byte[4];
        result[3] = (byte) (input >> 24 & 0xFF);
        result[2] = (byte) (input >> 16 & 0xFF);
        result[1] = (byte) (input >> 8 & 0xFF);
        result[0] = (byte) (input & 0xFF);
        return result;
    }

    public static byte[] fromInt16(int input) {
        byte[] result = new byte[2];
        result[0] = (byte) (input >> 8 & 0xFF);
        result[1] = (byte) (input & 0xFF);
        return result;
    }

    public static byte[] fromInt16Reversal(int input) {
        byte[] result = new byte[2];
        result[1] = (byte) (input >> 8 & 0xFF);
        result[0] = (byte) (input & 0xFF);
        return result;
    }

    public static String convertDoubleToStr(double value, String format) {
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(value);
    }
}

